Public Class ExTitleBar
    Public Event btnClose_Click()
    Public Event btnMin_Click()
    Public Event btnMax_Click()

#Region "Drawing Functions"
    Sub DrawRect(ByVal e As PaintEventArgs, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, _
    ByVal y2 As Single, ByVal offset As Single, ByVal pen As Pen)
        Dim pOffset As Point = New Point(2 * offset, 2 * offset)
        Dim p1 As Point = New Point(x1 + offset, y1)
        Dim p2 As Point = New Point(x2 - offset, y1)
        Dim p3 As Point = New Point(x2, y1 + offset)
        Dim p4 As Point = New Point(x2, y2 - offset)
        Dim p5 As Point = New Point(x2 - offset, y2)
        Dim p6 As Point = New Point(x1 + offset, y2)
        Dim p7 As Point = New Point(x1, y2 - offset)
        Dim p8 As Point = New Point(x1, y1 + offset)

        Dim pm1 As Point = New Point(x2 - 2 * offset, y1)
        Dim pm2 As Point = New Point(x2 - 2 * offset, y2 - 2 * offset)
        Dim pm3 As Point = New Point(x1, y2 - 2 * offset)
        Dim pm4 As Point = New Point(x1, y1)

        Dim p As Graphics = e.Graphics
        p.DrawLine(pen, p1, p2)
        p.DrawLine(pen, p3, p4)
        p.DrawLine(pen, p5, p6)
        p.DrawLine(pen, p7, p8)
        p.DrawArc(pen, New Rectangle(pm1, pOffset), 270, 90)
        p.DrawArc(pen, New Rectangle(pm2, pOffset), 0, 90)
        p.DrawArc(pen, New Rectangle(pm3, pOffset), 90, 90)
        p.DrawArc(pen, New Rectangle(pm4, pOffset), 180, 90)
    End Sub
    Sub DrawEdge(ByVal objControl As Control, ByVal e As PaintEventArgs, ByVal eWidth As Single, ByVal type As Byte)
        eWidth = 1
        Dim offset As Single = 3
        Dim x1 As Single = 0
        Dim y1 As Single = 0
        Dim x2 As Single = objControl.Width - eWidth - 1
        Dim y2 As Single = objControl.Height - eWidth - 1
        Dim a1 As Single = x1 + eWidth
        Dim b1 As Single = y1 + eWidth
        Dim a2 As Single = x2 + eWidth
        Dim b2 As Single = y2 + eWidth

        Dim p As Graphics = e.Graphics
        Dim myPen As Pen
        If type <> 1 Then
            '----draw bottom bar-----------------------
            Dim h = 10
            Dim w = x2 - a1

            Dim c1 As Single = a1 + eWidth
            Dim d1 As Single = y2 - h
            Dim c2 As Single = w - eWidth
            Dim d2 As Single = h

            Dim rect As RectangleF = New RectangleF(c1, d1, c2, d2)
            Dim c11 As Drawing.Color = Color.FromArgb(194, 216, 241)
            Dim c12 As Drawing.Color = Color.FromArgb(192, 216, 239)
            Dim rectBlend As Rectangle = New Rectangle(x1, y1, a2, b2)
            Dim mode As Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical
            Dim myFactors As Single() = {0.2F, 0.4F, 0.8F, 0.8F, 0.4F, 0.2F}
            Dim myPositions As Single() = {0.0F, 0.2F, 0.4F, 0.6F, 0.8F, 1.0F}
            Dim myBlend As New Drawing2D.Blend
            myBlend.Factors = myFactors
            myBlend.Positions = myPositions
            Dim lgBrush As New Drawing2D.LinearGradientBrush(rectBlend, c11, c12, mode)
            'p.FillRectangle(lgBrush, rect)
            '------------------------------------------------------------------------
            myPen = New Pen(Color.FromArgb(234, 240, 247)) 'inline
            myPen.Width = eWidth
            DrawRect(e, a1, b1, a2, b2, offset, myPen)

            myPen = New Pen(Color.FromArgb(204, 222, 243)) 'inline
            myPen.Width = eWidth
            p.DrawLine(myPen, c1 - eWidth, d1 + eWidth, c1 - eWidth, d1 + h - 2 * eWidth)

            myPen = New Pen(Color.FromArgb(193, 208, 222)) 'outline
            myPen.Width = eWidth
            DrawRect(e, x1, y1, x2, y2, offset, myPen)
        Else 'Parent Panel
            x1 = 0
            y1 = 0
            x2 = objControl.Width - 1
            y2 = objControl.Height + eWidth - 1
            a1 = x1 + eWidth - 1
            b1 = y1
            a2 = x2 - eWidth
            b2 = y2 - eWidth
            myPen = New Pen(Color.FromArgb(219, 244, 254)) 'inline
            myPen.Width = eWidth
            DrawRect(e, a1, b1, a2, b2, offset, myPen)

            myPen = New Pen(Color.FromArgb(59, 90, 130)) 'outline
            myPen.Width = eWidth
            DrawRect(e, x1, y1, x2, y2, offset, myPen)
        End If
    End Sub

#End Region 'Drawing Functions

    Private Sub ExTitleBar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim TopBrush As New Drawing2D.LinearGradientBrush(New Rectangle(1, 1, Me.Width - 1, Me.Height - 1), _
                Color.FromArgb(67, 71, 82), Color.FromArgb(58, 61, 69), Drawing2D.LinearGradientMode.Vertical)
        Dim BottomBrush As New Drawing2D.LinearGradientBrush(New Rectangle(1, 1, Me.Width - 1, Me.Height - 1), _
                        Color.FromArgb(47, 48, 48), Color.FromArgb(59, 59, 59), Drawing2D.LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(TopBrush, New RectangleF(0, 0, Me.Width, Me.Height / 4))
        e.Graphics.FillRectangle(BottomBrush, New RectangleF(0, Me.Height / 4, Me.Width, Me.Height))
        'DrawEdge(sender, e, 0.5, 1)
    End Sub

    Private Sub btnMin_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMin.Click
        RaiseEvent btnMin_Click()
    End Sub

    Private Sub btnMin_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMin.MouseDown
        btnMin.ImageIndex = 2
    End Sub

    Private Sub btnMin_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMin.MouseLeave
        btnMin.ImageIndex = 0
    End Sub

    Private Sub btnMin_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMin.MouseMove
        btnMin.ImageIndex = 1
    End Sub

    Private Sub btnClose_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        RaiseEvent btnClose_Click()
    End Sub

    Private Sub btnClose_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnClose.MouseDown
        btnClose.ImageIndex = 8
    End Sub

    Private Sub btnClose_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.MouseLeave
        btnClose.ImageIndex = 6
    End Sub

    Private Sub btnClose_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnClose.MouseMove
        btnClose.ImageIndex = 7
    End Sub

    Private _isMax As Boolean = False
    Public ReadOnly Property IsMax()
        Get
            Return _isMax
        End Get
    End Property
    Private Sub btnMax_Click1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMax.Click
        _isMax = Not _isMax
        RaiseEvent btnMax_Click()
    End Sub

    Private Sub btnMax_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMax.MouseDown
        If IsMax Then
            btnMax.ImageIndex = 5
        Else
            btnMax.ImageIndex = 11
        End If
    End Sub

    Private Sub btnMax_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMax.MouseLeave
        If _isMax Then
            btnMax.ImageIndex = 3
        Else
            btnMax.ImageIndex = 9
        End If
    End Sub

    Private Sub btnMax_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMax.MouseMove
        If _isMax Then
            btnMax.ImageIndex = 4
        Else
            btnMax.ImageIndex = 10
        End If
    End Sub

    Private Sub lblCaption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCaption.Click
        Me.OnClick(e)
    End Sub

    Private Sub lblCaption_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblCaption.DoubleClick
        _isMax = Not _isMax
        If _isMax Then
            btnMax.ImageIndex = 3
        Else
            btnMax.ImageIndex = 9
        End If
        Me.OnDoubleClick(e)
    End Sub
End Class
